/*
 * @author zzr
 * @date: 2025/10/12  15:30
 * @description: 输入一个链表，输出该链表中倒数第 K 个节点
 */
public class Demo6 {
    public class ListNode {
        int val;
        ListNode next;

        ListNode() {
        }

        ListNode(int val) {
            this.val = val;
        }

        ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }
    }

    public ListNode findKthToTail(ListNode head,int k) {

        if (k <= 0) {
            return null;
        }

        if (head == null) {
            return null;
        }

        ListNode fast = head;
        ListNode slow = head;
        while ((k - 1) != 0) {
            fast = fast.next;
            if (fast == null) {
                return null;
            }
            k--;
        }
        while (fast != null) {
            fast = fast.next;
            slow = slow.next;
        }
        return slow;
    }
}
